package es.udc.pfc.davidparedes.model.store;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import es.udc.pfc.davidparedes.model.dao.GenericDaoHibernate;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

@Repository("storeDao")
public class StoreDaoHibernate extends
		GenericDaoHibernate<Store, Long> implements StoreDao {

	public Store findByName(String name) throws InstanceNotFoundException {
		
		Store store = (Store) getSession().createQuery(
    			"SELECT t FROM Store t WHERE t.name = :name")
    			.setParameter("name", name)
    			.uniqueResult();
    	if (store == null) {
   			throw new InstanceNotFoundException(name, Store.class.getName());
    	} else {
    		return store;
    	}
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Store> findAll() {
		
		Calendar now = Calendar.getInstance();
		return getSession()
				  .createQuery(
				  "SELECT s FROM Store s WHERE "
				  + "s.deletionDate > :now "
				  + "OR s.deletionDate is null")
				  .setCalendar("now", now)
				  .list();
	}

}
